package com.cali.common.kt

import android.content.Context
import android.content.pm.PackageInfo
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import com.cali.libcore.R
import com.muddzdev.styleabletoast.StyleableToast
import me.yokeyword.fragmentation.SupportFragment


fun Context.toast(tip:String) {
//    Toast.makeText(this,tip,Toast.LENGTH_SHORT).show()
    StyleableToast.Builder(this)
        .text(tip)
        .gravity(Gravity.CENTER)
        .textColor(Color.WHITE)
        .backgroundColor(Color.GRAY)
        .show()
}

fun Context.format(name:Int, vararg items:String): String {
    //resources.getIdentifier(name,"string",this.packageName)
    return resources.getString(name, *items)
}

fun Context.format(name:Int, vararg items:Any): String {
    //resources.getIdentifier(name,"string",this.packageName)
    return resources.getString(name, *items)
}

fun Context.versionCode(): Int{
    return if (Build.VERSION.SDK_INT >= 28) {
        packageInfo()?.longVersionCode?.toInt()?:1
    }else{
        packageInfo()?.versionCode?:1
    }

}

fun Context.packageInfo(): PackageInfo? {
    val manager = packageManager
    return manager.getPackageInfo(packageName,0)
}

fun SupportFragment.param(vararg param: Pair<String, String>): SupportFragment {
    val bundle = Bundle()
    param.forEach {
        bundle.putString(it.first, it.second)
    }
    this.arguments = bundle
    return this
}

fun Bundle?.value(key: String) = this?.get(key)?.toString()?:""

fun Context.defaultTextSize(): Int {
    return this.resources.getDimensionPixelSize(R.dimen.text_default_sp)
}

fun Context.toolbarHeight(): Int {
    return this.resources.getDimensionPixelSize(R.dimen.dp_40)
}
